<?php
namespace App\Http\Controllers\Index;

use App\Logic\Code;
use App\Logic\finger\FilLogic;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\User;
use Illuminate\Support\Facades\Validator;

class FilController extends Controller
{



    /**
     * @OA\Post(
     *     path="/admin/filMeter",
     *     tags={"FIL"},
     *     summary="fil仪表",
     *      @OA\Parameter(
     *         name="Authorization",
     *         in="header",
     *         description="bearer+token"
     *     ),
     *    @OA\RequestBody(
     *         @OA\MediaType(
     *             mediaType="application/json",
     *             @OA\Schema(
     *                 @OA\Property(property="date", type="string", description="日期，默认请传入今日 Y-h-d"),
     *             )
     *         )
     *     ),
     *     @OA\Response(
     *         response="200",
     *         description="正常",
     *     )
     * )
     */
    public function meter(Request $request)
    {

        if($request->has('date')){
            $date = $request->input('date');
            $token = 'FIL';
            $type = 'mining';
            $start = strtotime($date);
            $start_time = date('Y-m-d H:i:s',$start);
            $end_time = date('Y-m-d H:i:s',$start+86400);
            $where_add_time[] = ['timestamp','>',$start_time];
            $where_add_time[] = ['timestamp','<',$end_time];

            //当日产出
            $the_day_out = DB::table('miner_balance')
                ->where('token',$token)
                ->where('type',$type)
                ->where($where_add_time)
                ->sum('amount');



            //当日审核通过提现币数量
            $where_check_time[] = ['check_time','>',$start_time];
            $where_check_time[] = ['check_time','<',$end_time];

            $withdraw_all = DB::table('withdraw as w')
                ->where('type','fil')
                ->where('token','FIL')
                ->where('state',1)
                ->where($where_check_time)
                ->sum('amount');

            $fil_all = DB::table('user')->sum('fil');

            $d =  Cache::get('configs')['mining']['start_interval']->val;
            $d = $d - 1;
            $time = time();
            $start_time = $time - 86400 * $d;
            $start_date = date('Y-m-d',$start_time);

            $power_all = DB::table('miner_host as mh')
                ->where('mh.add_time','<',$start_date)
                ->where('mh.end_time','>',$time)
                ->sum('hspace');

            return s_msg('',[
                'power_all' => $power_all,     //算力挖矿中算力数
                'fil_all' => $fil_all,          //持币余额
                'withdraw_all' => $withdraw_all,    //以提现总数
                'the_day_out' => $the_day_out,      //fil产出数
            ]);

        }else{
            return e_msg(123456,'请传入日期，date');
        }

    }


    /**
     * @OA\Post(
     *     path="/filProfit",
     *     tags={"FIL"},
     *     summary="fil_profit",
     *      @OA\Parameter(
     *         name="Authorization",
     *         in="header",
     *         description="bearer+token"
     *     ),
     *     @OA\Response(
     *         response="200",
     *         description="正常",
     *     )
     * )
     */
    public function profit(Request $request)
    {
        $device = DB::table('miner_user as mu')
            ->leftJoin('miner_host as mh','mh.mid','=','mu.mid')
            ->where('mu.user_name',$this->user['username'])
            ->count();
        $power = DB::table('miner_user as mu')
            ->leftJoin('miner_host as mh','mh.mid','=','mu.mid')
            ->where('mu.user_name',$this->user['username'])
            ->sum('mh.hspace');

        $already = DB::table('withdraw')
            ->where('user_name',$this->user['username'])
            ->where('state',1)
            ->sum('amount');

        $rel = $this->user['eth'];

        return s_msg('',[
            'total' => $rel + $already,
            'rel' =>  $rel,
            'already' =>  $already,
            'power' => $power,
            'device' => $device,
        ]);

    }


    /**
     * @OA\Post(
     *     path="/filTransfer",
     *     tags={"FIL"},
     *     summary="转给其他用户积分",
     *      @OA\Parameter(
     *         name="Authorization",
     *         in="header",
     *         description="bearer+token"
     *     ),
     *    @OA\RequestBody(
     *         @OA\MediaType(
     *             mediaType="application/json",
     *             @OA\Schema(
     *                 required={"user_name","amount","note"},
     *                 @OA\Property(property="user_name", type="string", description="用户名称或手机号"),
     *                 @OA\Property(property="amount", type="number", description="转账数量"),
     *                 @OA\Property(property="code", type="number", description="用户验证码"),
     *             )
     *         )
     *     ),
     *     @OA\Response(
     *         response="200",
     *         description="正常",
     *     )
     * )
     */
    public function transfer(Request $request)
    {
        //字段，验证码，接受人，转账数量，转账最小数量，手续费srt
        $post_data = $request->all();
        $scene = $request->path();
        return FilLogic::transfer($post_data,$this->user,$scene);

    }

    /**
     * @OA\Post(
     *     path="/admin/filTransfer",
     *     tags={"FIL"},
     *     summary="系统修改",
     *      @OA\Parameter(
     *         name="Authorization",
     *         in="header",
     *         description="bearer+token"
     *     ),
     *    @OA\RequestBody(
     *         @OA\MediaType(
     *             mediaType="application/json",
     *             @OA\Schema(
     *                 @OA\Property(property="id", type="string", description="用户username username"),
     *                 @OA\Property(property="amount", type="number", description="转账数量"),
     *                 @OA\Property(property="token", type="number", description="token = FIL or RED or POWER"),
     *             )
     *         )
     *     ),
     *     @OA\Response(
     *         response="200",
     *         description="正常",
     *     )
     * )
     */
    public function transfer_sys(Request $request)
    {
        //字段，验证码，接受人，转账数量，转账最小数量，手续费srt
        $post_data = $request->all();
        return FilLogic::transfer_sys($post_data);
    }


    /**
     * @OA\Post(
     *     path="/admin/filTransferList",
     *     tags={"FIL"},
     *     summary="转让列表",
     *      @OA\Parameter(
     *         name="Authorization",
     *         in="header",
     *         description="bearer+token"
     *     ),
     *    @OA\RequestBody(
     *         @OA\MediaType(
     *             mediaType="application/json",
     *             @OA\Schema(
     *                 required={"state"},
     *                 @OA\Property(property="coin_type", type="number", description="币种类型coin_type 只有一个 fil red"),
     *                 @OA\Property(property="key", type="string", description="搜索字段 电话号码 out_phone，用户名称 out_account"),
     *                 @OA\Property(property="val", type="string", description="搜索值"),
     *             )
     *         )
     *     ),
     *     @OA\Response(
     *         response="200",
     *         description="正常",
     *     )
     * )
     */
    /**
     * @OA\Post(
     *     path="/filTransferList",
     *     tags={"FIL"},
     *     summary="转让列表",
     *      @OA\Parameter(
     *         name="Authorization",
     *         in="header",
     *         description="bearer+token"
     *     ),
     *    @OA\RequestBody(
     *         @OA\MediaType(
     *             mediaType="application/json",
     *             @OA\Schema(
     *                 required={"state"},
     *                 @OA\Property(property="coin_type", type="number", description="币种类型coin_type 只有一个 fil"),
     *                 @OA\Property(property="key", type="string", description="搜索字段 电话号码 out_phone，用户名称 out_account"),
     *                 @OA\Property(property="val", type="string", description="搜索值"),
     *             )
     *         )
     *     ),
     *     @OA\Response(
     *         response="200",
     *         description="正常",
     *     )
     * )
     */
    public function transfer_list(Request $request)
    {
        $data = $request->all();

        $where = [];

        if($request->has('limit')){
            $limit = $request->input('limit');
        }else{
            $limit = 15;
        }

        if( isset($post_data['key']) && isset($post_data['val']) ){
            $key = $data['key'];
            if(!in_array($key,['out_account','out_phone'])){
                return e_msg(123456,'只能搜索用户名称out_account和电话号码out_phone');
            }
            $key = 'rf.'.$key;
            $where[] = [$key,'=',$data['val']];
        }

        if($request->has('coin_type')){
            $coin_type = $request->input('coin_type');
            $where[] = ['rf.coin_type','=',$coin_type];
        }
        if($request->has('type')){
            $type = $request->input('type');
            $where[] = ['rf.type','=',$type];
        }
        if(isset($this->user['id'])){
            $where[] = ['rf.out_account','=',$this->user['username']];
        }

         if($coin_type == 'power'){

            $list = Db::table('record_fil as rf')
                ->leftJoin('user as u','rf.out_account','=','u.username')
                ->where($where)
                ->select('rf.*','u.phone as out_phone','rf.out_phone as out_account')
                ->orderBy('id','desc')
                ->paginate($limit);

        }else{
            $list = Db::table('record_fil as rf')
                ->where($where)
                ->select('rf.*')
                ->orderBy('id','desc')
                ->paginate($limit);
        }

        return s_msg('',$list);

    }



}
